package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/kadycui/web/game"
	. "github.com/kadycui/web/model"
	"github.com/kadycui/web/utils"
	"net/http"
)

func Webhook(ctx *gin.Context) {
	headers := ctx.Request.Header
	sign := headers.Get("X-Te-Ops-Signature")

	//// 获取原始请求体数据
	requestBody, err := ctx.GetRawData()
	if err != nil {
		ctx.JSON(400, gin.H{"error": err.Error()})
		return
	}

	key := utils.GetEnv("KEY", "123456")
	// 打印请求体数据
	sha1 := utils.HmacSha1Encode(requestBody, key)
	log.Info("sign: ", sign)
	log.Info("sha1: ", sha1)

	if sign == sha1 {
		game.SyncGm(requestBody)
		response := Response{
			ReturnCode:    0,
			ReturnMessage: "success",
			Data: Data{
				FailList: []FailItem{
					{
						Index:   2,
						Message: "需要推送的玩家token信息不存在",
					},
					{
						Index:   4,
						Message: "push id not found",
					},
				},
			},
		}
		ctx.JSON(http.StatusOK, response)

	} else {
		response := Response{
			ReturnCode:    1,
			ReturnMessage: "sign",
			Data: Data{
				FailList: []FailItem{},
			},
		}
		ctx.JSON(http.StatusOK, response)

	}

}
